﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace prjKQXS_DLL
{
    public partial class frmServerCfg : DevExpress.XtraEditors.XtraForm
    {
        public frmServerCfg()
        {
            InitializeComponent();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(BienTc.connString);
            try
            {
                SqlCommand cmd = new SqlCommand("Select count(*) from SERVERCONFIG", conn);
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                if (cmd.ExecuteScalar().ToString() == "0")
                {
                    cmd = new SqlCommand("insert into SERVERCONFIG(Server,Username,Password,DbName) values(@Server,@Username,@Password,@DbName)", conn);
                    cmd.Parameters.AddWithValue("@Server", txtServer.Text);
                    cmd.Parameters.AddWithValue("@Username", txtUser.Text);
                    cmd.Parameters.AddWithValue("@Password", txtPass.Text);
                    cmd.Parameters.AddWithValue("@DbName", txtDbname.Text);
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    cmd.ExecuteNonQuery();
                }
                else
                {
                    cmd = new SqlCommand("update SERVERCONFIG set Server = @Server, Username = @Username,Password = @Password, DbName = @DbName", conn);
                    cmd.Parameters.AddWithValue("@Server", txtServer.Text);
                    cmd.Parameters.AddWithValue("@Username", txtUser.Text);
                    cmd.Parameters.AddWithValue("@Password", txtPass.Text);
                    cmd.Parameters.AddWithValue("@DbName", txtDbname.Text);
                    if (conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }
                    cmd.ExecuteNonQuery();
                }
                this.Close();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }

        private void frmServerCfg_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(BienTc.connString);
            try
            {
                SqlCommand cmd = new SqlCommand("select * from SERVERCONFIG", conn);
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    txtServer.Text = dr["Server"].ToString();
                    txtPass.Text = dr["Password"].ToString();
                    txtUser.Text = dr["Username"].ToString();
                    txtDbname.Text = dr["DbName"].ToString();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
    }
}